import { HttpService, Injectable, Logger } from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
const cheerio = require('cheerio');
import { INetProfitInfo } from './moutai.model';


@Injectable()
export class MoutaiService {
  private readonly logger = new Logger(MoutaiService.name);

  constructor(private readonly httpService: HttpService) {
  }

  getNetProfit(): Observable<INetProfitInfo> {
    const url = 'http://quotes.money.163.com/f10/zycwzb_600519.html#01c01';
    return this.httpService.get(url)
      .pipe(
        map((res) => {
          const $ = cheerio.load(res.data);
          const title = $('#scrollTable .col_l table tr').eq(11).find('td').text();
          const resultTrs = $('#scrollTable .col_r table tr');
          const dates = resultTrs.eq(0).children().map((i, el) => {
            return $(el).text();
          }).get();
          const values = resultTrs.eq(11).children().map((i, el) => {
            return $(el).text();
          }).get();

          return {title, dates, values};
        })
      );
  }
}
